<?php

namespace app\common\components;


use yii\web\Controller;

/**
 * Class BaseWebController
 * 集成常用方法，提供为所用Controller使用
 *
 * @package app\common\components
 */
class BaseWebController extends Controller
{

    public $enableCsrfValidation = false; //关闭Csrf

    public function __construct($id,  $module, array $config = [])
    {
        parent::__construct($id, $module, $config);
        $this->layout = false;
    }

    public function get($key, $default_val = '')
    {
        return \Yii::$app->request->get($key, $default_val);
    }

    public function post($key, $default_val = '')
    {
        return \Yii::$app->request->post($key, $default_val);
    }

    public function setCookie($name, $value, $expire = 0)
    {
        $cookie = \Yii::$app->response->cookies;
        $cookie->add(new \yii\web\Cookie([
            'name'   => $name,
            'value'  => $value,
            'expire' => $expire
        ]));
    }

    public function getCookie($name, $default_val = '')
    {
        $cookie = \Yii::$app->request->cookies;

        return $cookie->getValue($name, $default_val);
    }

    public function removeCookie($name)
    {
        $cookie = \Yii::$app->response->cookies;

        return $cookie->remove($name);
    }

    public function renderJson($data = [], $msg = 'ok', $code = 200)
    {
        header('Content-type:application/json');

        return json_encode([
            'msg'    => $msg,
            'code'   => $code,
            'data'   => $data,
            'req_id' => uniqid()
        ]);
    }

    //统一js回复
    public function renderJs($msg,$url)
    {
        return $this->renderPartial('@app/views/common/js',['msg'=>$msg ,'url'=>$url]);
    }
}
